﻿var core = {
    getElementStyle: {
        getAbsoluteLeft: function (obj) {
            var o = obj.get(0);
            oLeft = o.offsetLeft;
            while (o.offsetParent != null) {
                oParent = o.offsetParent;
                oLeft += oParent.offsetLeft;
                o = oParent;
            }
            return oLeft;
        },
        getAbsoluteTop: function (obj) {
            var o = obj.get(0);
            oTop = o.offsetTop;
            while (o.offsetParent != null) {
                oParent = o.offsetParent;
                oTop += oParent.offsetTop;
                o = oParent;
            }
            return oTop;
        },
        getWidth: function (obj) {
            return $(obj).css("width");
        },
        getHeight: function (obj) {
            return $(obj).css("height");
        }
    },
    event_stopPropagation: function (e) {
        var e = e || window.event;
        var stopP = function () {
            if (e && e.stopPropagation) {
                e.stopPropagation();
            } else {
                window.event.cancelBubble = true;
            }
        }
        return stopP;
    },
    extend: function (target, source) {
        for (var p in source) {
            if (source.hasOwnProperty(p)) {
                target[p] = source[p];
            }
        }
        return target;
    },
    compareObject: function (o1, o2) {
        if (typeof o1 != typeof o2) {
            return false;
        }
        if (typeof o1 == 'object') {
            for (var o in o1) {
                if (typeof o2[o] == 'undefined') return false;
                if (!core.compareObject(o1[o], o2[o])) return false;
            }
            return true;
        } else {
            return o1 === o2;
        }
    },
    getQueryString: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
};

/*
Event 处理类
*/
var eventManager = function () {

    this.listeners = {};

    this.doEvent = function (eventName, eventProperties) {
        if (!this.listeners[eventName]) {
            return;
        }
        if (this.listeners[eventName].length == 0) {
            console.log("事件:[" + eventName + "]已经没有函数指针了,请重新添加一个指针来执行");
            return;
        }
        for (var i = 0; i < this.listeners[eventName].length; i++) {
            this.listeners[eventName][i](eventProperties);
        }
    }

    this.addListener = function (eventName, callback) {
        if (this.listeners[eventName] == undefined) {
            this.listeners[eventName] = [];
        }

        this.listeners[eventName].push(callback);
    }

    this.removeListener = function (eventName, callback) {
        if (!this.listeners[eventName]) {
            return;
        }
        for (var i = 0; i < this.listeners[eventName].length; i++) {
            if (this.listeners[eventName][i] == callback) {
                var index = this.listeners[eventName].findItem(callback);
                this.listeners[eventName] = this.listeners[eventName].deleteItem(index);
                return;
            }
        }
    }
}


/* index从0开始 */
Array.prototype.deleteItem = function (index) {
    if (index < 0 || index >= this.length) {
        return this;
    }
    else {
        return this.slice(0, index).concat(this.slice(index + 1, this.length));
    }
}

/* index从0开始,如果不存在则返回-1 */
Array.prototype.findItem = function (item) {
    var index = -1;
    if (this.length > 0) {
        for (var i = 0; i < this.length; i++) {
            if (item == this[i]) {
                index = i;
                break;
            }
        }
    }
    return index;
}

/*去除数组里的重复项 返回新的数组*/
Array.prototype.unique = function () {
    var temp = new Array();
    this.sort();
    for (i = 0; i < this.length; i++) {
        if (typeof this[i] == 'object') {
            if (core.compareObject(this[i], this[i + 1])) {
                continue;
            }
        } else {
            if (this[i] == this[i + 1]) {
                continue;
            }
        }

        temp[temp.length] = this[i];
    }
    return temp;
}

/*判断某项在数组里是否存在  true代表存在 false代表不存在*/
Array.prototype.isExist = function (val) {
    var bl = false;
    for (var i = 0; i < this.length; i++) {
        if (val == this[i]) {
            bl = true;
        }
    }
    return bl;
}

/*克隆数组*/
Array.prototype.clone = function () {
    var arr = [];
    for (var i = 0; i < this.length; i++) {
        arr.push(this[i]);
    }
    return arr;
}